package org.jeecg.modules.online;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.util.JeecgDataAutorUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.online.auth.service.IOnlAuthDataService;
import org.jeecg.modules.online.auth.service.IOnlAuthPageService;
import org.jeecg.modules.online.cgform.a.a;
import org.jeecg.modules.online.cgform.entity.OnlCgformField;
import org.jeecg.modules.online.cgform.entity.OnlCgformHead;
import org.jeecg.modules.online.cgform.mapper.OnlCgformFieldMapper;
import org.jeecg.modules.online.cgform.mapper.OnlCgformHeadMapper;
import org.jeecg.modules.online.cgform.mapper.OnlineMapper;
import org.jeecg.modules.online.cgform.model.TreeModel;
import org.jeecg.modules.online.cgform.model.e;
import org.jeecg.modules.online.cgform.model.g;
import org.jeecg.modules.online.cgform.model.h;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.regex.Pattern;


@Service("mesOnlineQrery")
@Primary
public class MesOnlineQrery extends org.jeecg.modules.online.cgform.service.a.c {
    private static final Logger a = LoggerFactory.getLogger(MesOnlineQrery.class);
    private static final String b = "0";
    @Autowired
    private OnlCgformFieldMapper onlCgformFieldMapper;
    @Autowired
    private OnlCgformHeadMapper cgformHeadMapper;
    @Autowired
    private IOnlAuthDataService onlAuthDataService;
    @Autowired
    private IOnlAuthPageService onlAuthPageService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @Autowired
    private OnlineMapper onlineMapper;

    public MesOnlineQrery() {
    }

    public static void a(String var0, List<OnlCgformField> var1, StringBuffer var2) {
        if (var1 != null && var1.size() != 0) {
            var2.append("SELECT ");
            int var3 = var1.size();
            boolean var4 = false;

            for(int var5 = 0; var5 < var3; ++var5) {
                OnlCgformField var6 = (OnlCgformField)var1.get(var5);
                if (org.jeecg.modules.online.cgform.b.b.b.equals(var6.getDbIsPersist())) {
//                    if ("id".equals(var6.getDbFieldName())) {
//                        var4 = true;
//                    }

                    if ("cat_tree".equals(var6.getFieldShowType()) && oConvertUtils.isNotEmpty(var6.getDictText())) {
                        var2.append(var6.getDictText() + ",");
                    }

                    if (var5 == var3 - 1) {
                        var2.append(var6.getDbFieldName() + " ");
                    } else {
                        var2.append(var6.getDbFieldName() + ",");
                    }
                }
            }

            String var7 = var2.substring(var2.length() - 1);
            if (",".equals(var7)) {
                var2.deleteCharAt(var2.length() - 1);
            }

//            if (!var4) {
//                var2.append(",id");
//            }
        } else {
            var2.append("SELECT id");
        }

        var2.append(" FROM " + f(var0));
    }

    public static String f(String var0) {
        return Pattern.matches("^[a-zA-z].*\\$\\d+$", var0) ? var0.substring(0, var0.lastIndexOf("$")) : var0;
    }

    private List<OnlCgformField> a(String var1, List<OnlCgformField> var2, List<String> var3, List<String> var4) {
        LoginUser var5 = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        String var6 = var5.getId();
        ArrayList var7 = new ArrayList();
        Iterator var8 = var2.iterator();

        while(var8.hasNext()) {
            OnlCgformField var9 = (OnlCgformField)var8.next();
            String var10 = var9.getDbFieldName();
            if (var3.indexOf(var10) >= 0) {
                var7.add(var9);
            }
        }

        List var11 = this.onlAuthPageService.queryListHideColumn(var6, var1);
        return this.a(var11, true, var7, var4);
    }

    private List<OnlCgformField> a(List<String> var1, boolean var2, List<OnlCgformField> var3, List<String> var4) {
        ArrayList var5 = new ArrayList();
        boolean var6 = true;
        if (var1 == null || var1.size() == 0 || var1.get(0) == null) {
            var6 = false;
        }

        Iterator var7 = var3.iterator();

        while(true) {
            while(var7.hasNext()) {
                OnlCgformField var8 = (OnlCgformField)var7.next();
                String var9 = var8.getDbFieldName();
                if (var4 != null && var4.contains(var9)) {
                    var8.setIsQuery(1);
                    var5.add(var8);
                } else {
                    if (var2) {
                        if (var8.getIsShowList() != 1) {
                            if (oConvertUtils.isNotEmpty(var8.getMainTable()) && oConvertUtils.isNotEmpty(var8.getMainField())) {
                                var5.add(var8);
                            }
                            continue;
                        }
                    } else if (var8.getIsShowForm() != 1) {
                        continue;
                    }

                    if (var6) {
                        if (this.b(var9, var1)) {
                            var5.add(var8);
                        }
                    } else {
                        var5.add(var8);
                    }
                }
            }

            return var5;
        }
    }

    public boolean a(String var1, List<OnlCgformField> var2) {
        boolean var3 = false;
        Iterator var4 = var2.iterator();

        while(var4.hasNext()) {
            OnlCgformField var5 = (OnlCgformField)var4.next();
            if (oConvertUtils.camelToUnderline(var1).equals(var5.getDbFieldName())) {
                var3 = true;
                break;
            }
        }

        return var3;
    }

    private String a(List<OnlCgformField> var1, Map<String, Object> var2) {
        Object var3 = var2.get("column");
        ArrayList var4 = new ArrayList();
        String var7;
        h var8;
        if (var3 != null && !"id".equals(var3.toString())) {
            String var12 = var3.toString();
            Object var14 = var2.get("order");
            var7 = "desc";
            if (var14 != null) {
                var7 = var14.toString();
            }

            var8 = new h(var12, var7);
            var4.add(var8);
        } else {
            Iterator var5 = var1.iterator();

            while(var5.hasNext()) {
                OnlCgformField var6 = (OnlCgformField)var5.next();
                if ("1".equals(var6.getSortFlag())) {
                    var7 = var6.getFieldExtendJson();
                    var8 = new h(var6.getDbFieldName());
                    if (var7 != null && !"".equals(var7)) {
                        JSONObject var9 = JSON.parseObject(var7);
                        String var10 = var9.getString("orderRule");
                        if (var10 != null && !"".equals(var10)) {
                            var8.setRule(var10);
                            var4.add(var8);
                        }
                    }
                }
            }

            if (var4.size() == 0) {
                h var11 = h.a();
                var4.add(var11);
            }
        }

        ArrayList var13 = new ArrayList();
        Iterator var15 = var4.iterator();

        while(var15.hasNext()) {
            h var16 = (h)var15.next();
            if (this.a(var16.getColumn(), var1)) {
                String var17 = var16.getRealSql();
                var13.add(var17);
            }
        }

        return " ORDER BY " + String.join(",", var13);
    }

    private boolean b(String var1, List<String> var2) {
        boolean var3 = true;

        for(int var4 = 0; var4 < var2.size(); ++var4) {
            String var5 = (String)var2.get(var4);
            if (!oConvertUtils.isEmpty(var5)) {
                String var6 = var5.substring(var5.lastIndexOf(":") + 1);
                if (!oConvertUtils.isEmpty(var6) && var6.equals(var1)) {
                    var3 = false;
                }
            }
        }

        return var3;
    }

    public e getQueryInfo(OnlCgformHead head, Map<String, Object> params, List<String> needList) {
        String var4 = head.getTableName();
        String var5 = head.getId();
        LambdaQueryWrapper<OnlCgformField> var6 = new LambdaQueryWrapper();
        var6.eq(OnlCgformField::getCgformHeadId, var5);
        var6.eq(OnlCgformField::getDbIsPersist, org.jeecg.modules.online.cgform.b.b.b);
        var6.orderByAsc(OnlCgformField::getOrderNum);
        List var7 = this.list(var6);
        new ArrayList();
        List var9 = head.getSelectFieldList();
        List var8;
        if (var9 != null && var9.size() > 0) {
            var8 = this.a(var5, var7, var9, needList);
        } else {
            var8 = this.queryAvailableFields(var5, var4, true, var7, needList);
        }

        StringBuffer var10 = new StringBuffer();
        a(var4, var8, var10);
        LoginUser var11 = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        String var12 = var11.getId();
        List var13 = this.onlAuthDataService.queryUserOnlineAuthData(var12, var5);
        if (var13 != null && var13.size() > 0) {
            JeecgDataAutorUtils.installUserInfo(this.sysBaseAPI.getCacheUser(var11.getUsername()));
        }

        org.jeecg.modules.online.config.d.b var14 = new org.jeecg.modules.online.config.d.b("t.");
        var14.setNeedList(needList);
        var14.setSubTableStr(head.getSubTableStr());
        List var15 = org.jeecg.modules.online.cgform.d.b.g(var7);
        String var16 = var14.a(var15, params, var13);
        Map var17 = var14.getSqlParams();
        if (var16.trim().length() > 0) {
            var10.append(" t ").append(" where  ").append(var16);
        }

//        String var18 = this.a(var7, params);
//        var10.append(var18);
        e var19 = new e(var10.toString(), var17);
        var19.setFieldList(var8);
        return var19;
    }

    public Map<String, Object> queryAutolistPage(OnlCgformHead head, Map<String, Object> params, List<String> needList) {
        e var4 = this.getQueryInfo(head, params, needList);
        String var5 = var4.getSql();
        Map var6 = var4.getParams();
        List var7 = var4.getFieldList();
        HashMap var8 = new HashMap(5);
        Integer var9 = params.get("pageSize") == null ? 10 : Integer.parseInt(params.get("pageSize").toString());
        if (var9 == -521) {
            List var10 = this.onlineMapper.selectByCondition(var5, var6);
            if (var10 != null && var10.size() != 0) {
                var8.put("total", var10.size());
                var8.put("fieldList", var7);
                var8.put("records", org.jeecg.modules.online.cgform.d.b.d(var10));
            } else {
                var8.put("total", 0);
                var8.put("fieldList", var7);
            }
        } else {
            Integer var14 = params.get("pageNo") == null ? 1 : Integer.parseInt(params.get("pageNo").toString());
            Page var11 = new Page((long)var14, (long)var9);
            IPage var12 = this.onlineMapper.selectPageByCondition(var11, var5, var6);
            var8.put("total", var12.getTotal());
            List var13 = org.jeecg.modules.online.cgform.d.b.d(var12.getRecords());
            this.handleLinkTableDictData(head.getId(), var13);
            var8.put("records", var13);
        }

        return var8;
    }
}
